package com.jindi.appuser.controller.manage;

import com.jindi.appuser.common.*;
import com.jindi.appuser.domain.App;
import com.jindi.appuser.domain.WinningRecord;
import com.jindi.appuser.domain.YysPrice;
import com.jindi.appuser.service.AppService;
import com.jindi.appuser.service.PriceService;
import com.jindi.appuser.service.WinningRecordService;
import com.jindi.appuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("app/user_yys")
public class AdminController {

    @Autowired
    private WinningRecordService winningRecordService;
    @Autowired
    private PriceService priceService;
    @Autowired
    private AppService appService;
    @Autowired
    private UserService userService;
    @PostMapping("login.do")
    public ServerResponse login(String username, String password, HttpServletRequest request){
        String ip = HttpRequest.getIPAddr(request);
        if(!"admin".equals(username)||!"yys5211314".equals(password)){
            return ServerResponse.createByErrorMessage("用户名或密码错误");
        }else{
            String token = RandomString.getRandomString(8);
            Jedis jedis = new Jedis("127.0.0.1");
            jedis.auth("123456");
            jedis.set("yys_admin_token",token);
            jedis.expire("yys_admin_token",120000);
            jedis.close();
            return ServerResponse.createBySuccess(token);
        }
    }

    @PostMapping("login_out.do")
    public ServerResponse loginOut(String token, HttpServletRequest request){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(jedis.exists("yys_admin_token")){
            jedis.del("yys_admin_token");
            jedis.close();
        }
        return ServerResponse.createBySuccess("退出成功");
    }
    @PostMapping("user_list.do")
    public ServerResponse userList(String token,
                                   String name,
                                   @RequestParam(value = "pageNum",defaultValue = "1",required = false) String pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "10",required = false)String pageSize
                                   ){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.userList(name,pageNum,pageSize);

    }

    @PostMapping("user_info.do")
    public ServerResponse userInfo(String token,String uid){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.getUserInfo(uid);

    }
/**
 *
 *      String strDate="2015-04-22 12:12:32";
 *         //注意：SimpleDateFormat构造函数的样式与strDate的样式必须相符
 *         SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间
 *         //必须捕获异常
 *         try {
 *             Date date=sDateFormat.parse(strDate);
 *             System.out.println(date);
 *         } catch(ParseException px) {
 *             px.printStackTrace();
 *         }
 * */
    @PostMapping("user_update.do")
    public ServerResponse userUpdate(String token,
                                   String uid,String count){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.userUpdate(uid,count);
    }

    @PostMapping("user_update_time.do")
    public ServerResponse userUpdateTime(String token,
                                     String uid,String time){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.userUpdateTime(uid,time);
    }


    /*
    @PostMapping("order_list.do")
    public ServerResponse userOrder(String token,
                                    String name,
                                    @RequestParam(value = "pageNum",defaultValue = "1",required = false) String pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "10",required = false)String pageSize
    ){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.orderList(name,pageNum,pageSize);

    }*/
    @PostMapping("order_list.do")
    public ServerResponse orderListName(String token,
                                    String name,
                                    @RequestParam(value = "pageNum",defaultValue = "1",required = false) String pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "10",required = false)String pageSize
    ){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.orderListName(name,pageNum,pageSize);

    }

    //订单统计
    @PostMapping("order_count.do")
    public ServerResponse orderList(String token){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.countOrder();
    }

    //订单数
    @PostMapping("order_count_num.do")
    public ServerResponse orderListNum(String token){
      if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.countOrderNum();
    }
    //抽奖
    @PostMapping("winning_record_list.do")
    public ServerResponse winningRecordList(String token,
                                            @RequestParam(value = "name",required = false)String name,
                                            @RequestParam(value = "pageSize",defaultValue = "20")String pageSize,
                                            @RequestParam(value = "pageNum",defaultValue = "1")String pageNum){
       /*if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }*/
        return userService.winningRecordList(name,Integer.parseInt(pageSize),Integer.parseInt(pageNum));
    }

    //修改奖品状态
    @PostMapping("winning_record_update.do")
    public ServerResponse winningRecordUpdate(String token,String doType,String id){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.winningRecordUpdate(doType,id);
    }

    //抽奖，中奖统计
    @PostMapping("winning_record_info.do")
    public ServerResponse winningRecordInfo(String token, String id){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.winningRecordInfo(id);
    }


    //注册人数统计
    @PostMapping("user_count.do")
    public ServerResponse userCount(String token){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.countUserList();
    }

    //价格
    @PostMapping("price_list.do")
    public ServerResponse priceList(String token){
       if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return priceService.priceList();
    }

    //获取单个价格信息
    @PostMapping("price_info.do")
    public ServerResponse priceInfo(String token,String id){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return priceService.priceInfo(id);
    }
    //修改单个价格信息
    @PostMapping("price_update.do")
    public ServerResponse priceUpdate(String token, YysPrice yysPrice){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return priceService.priceUpdate(yysPrice);
    }


    //套餐价格修改
    //app详情
    @PostMapping("app_info.do")
    public ServerResponse yysPriceInfo(String token){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return appService.info();
    }

    //app修改（价格，QQ，邮箱）
    @PostMapping("app_update.do")
    public ServerResponse appUpdate(String token, App app){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return appService.update(app);
    }


    //给中奖者充值（更改中奖状态）

    //关闭/开启抽奖 doType--1开启，0关闭
    @PostMapping("close_drow.do")
    public ServerResponse closeDrow(String token,String doType){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return appService.closeDrow(doType);
    }

    //封禁用户
    @PostMapping("del_user.do")
    public ServerResponse delUser(String token,String uid){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.delUser(uid);
    }


    //查看用户收货地址
    @PostMapping("user_address_list.do")
    public ServerResponse userAddressList(String token,String uid){
        if(EmptyUtil.isEmpty(token)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Jedis jedis = new Jedis("127.0.0.1");
        jedis.auth("123456");
        if(!jedis.exists("yys_admin_token") || !token.equals(jedis.get("yys_admin_token"))){
            return ServerResponse.createByErrorMessage("请重新登录");
        }
        return userService.userAddressList(uid);
    }

}
